import {
  get
} from "../../utils/service"

const app = getApp()

const optSeason = [{
    text: '春',
    value: 'spring'
  },
  {
    text: '夏',
    value: 'summer'
  },
  {
    text: '秋',
    value: 'autumn'
  },
  {
    text: '冬',
    value: 'winter'
  },
]

const optCollect = [{
    text: '不限',
    value: '0'
  },
  {
    text: '已收藏',
    value: '1'
  },
  {
    text: '未收藏',
    value: '2'
  },
]

Page({
  data: {
    pageData: [],
    pageSize: 10,
    pageNum: 1,
    refreshLoading: false,
    reachLoading: false,
    reachNoMore: false,
    collected: '0',
    optCollect: optCollect,
    optSeason: optSeason,
    selectedSeason: [],
    optTypes: [],
    selectedTypes: []
  },
  onClick: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id,
    })
  },
  onLoad: function (options) {
    const {
      pageSize,
      pageNum
    } = this.data
    this.getTypes()
    this.search({
      pageSize,
      pageNum
    })
  },
  onSeasonChange: function (event) {
    this.setData({
      selectedSeason: event.detail
    })
  },
  onCollectChange: function (event) {
    this.setData({
      collected: event.detail
    })
    this.search({
      pageNum: 1,
      pageSize: this.data.pageSize,
      reset: true
    })
  },
  onTypeChange: function (event) {
    this.setData({
      selectedTypes: event.detail
    })
  },
  onParamConfirm: function (event) {
    const id = event.currentTarget.dataset.id
    this.selectComponent(`#${id}`).toggle()
    this.search({
      pageNum: 1,
      pageSize: this.data.pageSize,
      reset: true
    })
  },
  getTypes: function () {
    const self = this
    get(`${app.globalData.host}/clothing/options`, {}, {
      success: function (data) {
        const c = data.categories
        if (c) {
          self.setData({
            optTypes: (c.list || []).map(i => ({
              text: i.name,
              value: i.id
            }))
          })
        }
      }
    })
  },
  search: function ({
    pageNum,
    pageSize,
    complete,
    reset
  }) {
    var self = this
    var param = {
      type: 'platform',
      havePicture: true,
      availabel: true
    }
    if (this.data.collected === '1') {
      param.collected = true
    } else if (this.data.collected === '2') {
      param.collected = false
    }
    if (this.data.selectedSeason) {
      param.seasons = this.data.selectedSeason.join(',')
    }
    if (this.data.selectedTypes) {
      param.typeIds = this.data.selectedTypes.join(',')
    }
    console.info("===== library param ===== ", param, reset)
    get(`${app.globalData.host}/clothing/library/page/${pageSize}/${pageNum}`, param, {
      success: function (data) {
        const total = data.total
        const pageData = data.pageData.map(i => {
          i.pltPrice = (i.pltPrice / 100).toFixed(2)
          i.cadPrice = (i.cadPrice / 100).toFixed(2)
          return i
        })
        //console.info("========== reset =======", reset, pageData)
        if (reset) {
          self.setData({
            pageData: pageData,
            reachNoMore: pageData.length >= total //到达最后一页停止
          })
          return
        }
        let selfPageData = self.data.pageData || []
        selfPageData = selfPageData.concat(pageData)
        //console.info("====library====", selfPageData)
        self.setData({
          pageData: selfPageData,
          reachNoMore: selfPageData.length >= total //到达最后一页停止
        })
        if (complete) complete()
      }
    })
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    if (this.data.reachNoMore) return
    this.setData({
      pageNum: this.data.pageNum + 1,
      reachLoading: true
    })
    const self = this
    this.search({
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum + 1,
      complete: function () {
        self.setData({
          reachLoading: false
        })
      }
    })
  },
  onPullDownRefresh: function () {
    console.info("=======library pullDown=====")
    const {
      pageSize
    } = this.data
    this.setData({
      pageNum: 1,
      refreshLoading: true
    })
    const self = this
    this.search({
      pageSize,
      pageNum: 1,
      complete: function () {
        self.setData({
          refreshLoading: false
        })
      }
    })
  },
})